<?php
// $objResponse->addAlert('Response: I am here!');return $objResponse;
class User extends Controller {
	
	var $fields;
	
	function setVars()
	{
		$sessionFields = (array) $this->session->userdata();
		$this->uid = (object) $this->session->userdata('uid');
		
		$this->fields = array(
			'Alias' => FALSE,
			'Password' => FALSE,
			'First_Name' => FALSE,
			'Last_Name' => FALSE,
			'Company' => FALSE,
			'Street' => FALSE,
			'City' => FALSE,
			'State' => FALSE,
			'ZIP' => FALSE,
			'ZIP4' => FALSE,
			'Country' => FALSE,
			'Phone' => FALSE,
			'Cell' => FALSE,
			'Fax' => FALSE,
			'eMail' => FALSE,
			'Yahoo' => FALSE,
			'MSN' => FALSE,
			'ICQ' => FALSE,
			'AIM' => FALSE,
			'url' => FALSE,
			'Birthday' => FALSE,
			'Notes' => FALSE);
		
		#Loading the fields
		$this->fields = $this->dbstringit->key_intersect($sessionFields,$this->fields);
	}
	
	
	function User()
	{
		parent::Controller();
		$this->load->library('dbStringIt');
		
		$this->load->library('xajax');
		#debug
		//$this->xajax->debugOn();
      $this->xajax->registerFunction(array('getContent',&$this,'getContent'));
		$this->xajax->registerFunction(array('axPassword',&$this,'axPassword'));
      $this->xajax->processRequests();
		
		$this->setVars();
		$this->output->enable_profiler(TRUE);
		log_message('debug', "User Controler Class Initialized");
	}
	
	function getContent($newContent)
	{
		$sForm = $this->axContent($newContent);
		$objResponse = new xajaxResponse();
		$objResponse->addAssign("ContentDiv","innerHTML", $sForm);
		
		return $objResponse;
	}
	
	
	function userMenu()
	{
		//$viewarray = $this->dbstringit->UpdateStrings($this->fields);
		//$menu[] = $viewarray;
		$menu[] = "
		<div id=\"MenuForm\"></div>
		<form action=\"post\">
		<input type=\"button\" value=\"Name\" onclick=\"xajax_getContent('name');\" /><br />
		<input type=\"button\" value=\"Password\" onclick=\"xajax_getContent('password');\" /><br />
		</form>
		";
		include 'menu.php';
		return $menu;
	}
	function axPassword($aFormValues)
	{
		$timeout = 4000;
		$objResponse = new xajaxResponse();
		
		if(array_key_exists("password",$aFormValues))
		{
			
			$CI =& get_instance();
			
			$tmppost = $_POST;
			$_POST = '';
			foreach($aFormValues as $k => $v)
			{
				$_POST[$k] = $v;
			}
			$pass = $CI->input->post('password');
			
			$CI->load->library('validation');
			$rules['password'] = "trim|xss_clean|required|alpha_numeric|min_length[5]|max_length[20]";
			$rules['confirm']	= "trim|xss_clean|required|matches[password]";
			$CI->validation->set_rules($rules);
			if ($CI->validation->run() !== FALSE)
			{
				
				$uid = $CI->session->userdata('uid');
				$data = array('Password' => $pass);
				$CI->db->where('uid', $uid);
				$CI->db->update('Account', $data);
				
				$CI->msg->setMsg('Your Password has been updated.');
				$_POST = $tmppost;
				$msg = $CI->msg->getMsg();
				$objResponse->addAssign("ContentDiv","innerHTML",$this->axContent('other'));
				$objResponse->addAssign("MessageDiv","innerHTML",$msg->html);
				$objResponse->addScript("document.getElementById('MessageDiv').style.display='inline'; 
					setTimeout(\"document.getElementById('MessageDiv').style.display='none'\",$msg->time); ");
			}else{
				$msg = $CI->validation->error_string;
				$_POST = $tmppost;
				$CI->msg->setError($msg);
				$msg = $CI->msg->getMsg();
				$objResponse->addAssign("MessageDiv","innerHTML",$msg->html);
				$objResponse->addScript("document.getElementById('MessageDiv').style.display='inline'; 
					setTimeout(\"document.getElementById('MessageDiv').style.display='none'\",$msg->time); ");
			}
			$_POST = $tmppost;
		}
		else
		{
			$sForm = $this->axContent('passowrd');
			$objResponse->addAssign("MessageDiv","innerHTML",$sForm);
		}
		
		$objResponse->addAssign("formWrapper","style.backgroundColor", "white");
		
		return $objResponse;
	}
	
	function axName($aFormValues)
	{
		$objResponse = new xajaxResponse();
		
		if(array_key_exists("First_Name",$aFormValues))
		{
			$CI =& get_instance();

			$tmppost = $_POST;
			$_POST = '';
			foreach($aFormValues as $k => $v)
			{
				$_POST[$k] = $v;
			}
			$CI->load->library('validation');
			$rules['First_Name'] = "trim|xss_clean|required|alpha_numeric";
			$rules['First_Name']	= "trim|xss_clean|required|alpha_numeric";
			$CI->validation->set_rules($rules);
			if ($CI->validation->run() !== FALSE)
			{
				$First_Name = $CI->input->post('First_Name');
				$Last_Name = $CI->input->post('Last_Name');
				$uid = $CI->session->userdata('uid');
				$data = array('First_Name' => $First_Name, 'Last_Name' => $Last_Name);
				$CI->db->where('uid', $uid);
				$CI->db->update('Account', $data);
				
				$CI->msg->setMsg('Your Name has been updated.');
				$_POST = $tmppost;
				$msg = $CI->msg->getMsg();
				$objResponse->addAssign("ContentDiv","innerHTML",$this->axContent('other'));
				$objResponse->addAssign("MessageDiv","innerHTML",$msg->html);
				$objResponse->addScript("document.getElementById('MessageDiv').style.display='inline'; 
					setTimeout(\"document.getElementById('MessageDiv').style.display='none'\",$msg->time); ");
			}else{
				$msg = $CI->validation->error_string;
				$_POST = $tmppost;
				$CI->msg->setError($msg);
				$msg = $CI->msg->getMsg();
				$objResponse->addAssign("MessageDiv","innerHTML",$msg->html);
				$objResponse->addScript("document.getElementById('MessageDiv').style.display='inline'; 
					setTimeout(\"document.getElementById('MessageDiv').style.display='none'\",$msg->time); ");
			}
			$_POST = $tmppost;
		}
		else
		{
			$sForm = $this->axContent('name');
			$objResponse->addAssign("MessageDiv","innerHTML",$sForm);
		}
		
		$objResponse->addAssign("formWrapper","style.backgroundColor", "white");
		
		return $objResponse;
	}
	
	
	function axContent($newContent)
	{
		$sForm = "No content found";
		if($newContent == 'name')
		{
			$sForm = "<fieldset><legend accesskey=\"P\" tabindex=\"1\">Name</legend>";
			$sForm .= '<form id="Form">';
			
			$sForm .= '<table><tr>';
			$sForm .= '<td>First Name:<br />';
			$sForm .= '<input type="text" name="First_Name" size="10" /></td>';
			
			$sForm .= '<td>Last Name:<br />';
			$sForm .= '<input type="test" name="Last_Name" size="10" /></td>';
			
			$sForm .= '</tr><tr><td colspan="2" style="text-align:right;">';
			
			$sForm .="<div class=\"submitDiv\"><input  class=\"submitDiv\" type=\"submit\" value=\"Save\"  id=\"submitButton\" onclick=\"xajax_axPassword(xajax.getFormValues('Form')); return false;\" /></div>";
			$sForm .= '<div id="submittedDiv" style=" margin: 3px;"></div>';
			
			$sForm .= '</td></tr></table></form>';
			$sForm .= "</fieldset>";
		}
		elseif($newContent == 'password')
		{
			$sForm = "<fieldset><legend accesskey=\"P\" tabindex=\"1\">Change Password</legend>\n";
			$sForm .= '<form id="Form">';

			$sForm .= '<table><tr>';
			$sForm .= '<td>Password:<br />';
			$sForm .= '<input type="password" name="password" size="10" /></td>';
			
			$sForm .= '<td>Confirm:<br />';
			$sForm .= '<input type="password" name="confirm" size="10" /></td>';
			
			$sForm .= '</tr><tr><td colspan="2" style="text-align:right;">';

			$sForm .="<div class=\"submitDiv\"><input  class=\"submitDiv\" type=\"submit\" value=\"Save\"  id=\"submitButton\" onclick=\"xajax_axPassword(xajax.getFormValues('Form')); return false;\" /></div>";
			$sForm .= '<div id="submittedDiv" style=" margin: 3px;"></div>';
			
			$sForm .= '</td></tr></table></form>';
			$sForm .= "</fieldset>";				
		}
		elseif($newContent == 'other')
		{
			$sForm = "<fieldset><legend accesskey=\"P\" tabindex=\"1\">Account Info</legend>\n";
			$sForm .='<form id="Form">';
			$sForm .="<div class=\"submitDiv\" style=\"width: ; text-align: right;\";>";
			$sForm .="<input style=\"width: ; text-align: center;\";  class=\"submitDiv\" type=\"submit\"  value=\"Save Changes\"  id=\"submitButton\" onclick=\"xajax_AccountData(xajax.getFormValues('Form')); return false;\" /></div>";
			$sForm .= "<input type=\"hidden\" name=\"other\" />";
			$sForm .= '<div id="submittedDiv" style=" margin: 3px;"></div>';
			
			
			$this->setVars();
			
			foreach($this->fields as $k => $v)
			{
				$sForm .= '<div>';
				if($k != 'Password')
				{
					$sForm .= "<input style=\"text-align: right;\" type=\"text\" size=\"10\" readonly  value=\"$k\" />";
					$sForm .= "<input type=\"text\" name=\"$k\" value=\"$v\" />";
				}
				$sForm .= '</div>';
			}
			$sForm .= '</form></fieldset>';
		}
		return $sForm;
	}
	
	
	function index() {

		
		$tplbody = '';
		
		$tpl['tpljs']['xajax'] = $this->xajax->getjavascript(null, '/public/js/xajax.js');
		$tpl['tpltitle'] = "User Settings";
		$tpl['tplhead'] = "";
		$tpl['tplmenu'] = $this->userMenu();
		$tpl['tplbody'] = $tplbody;
		$this->load->view('layout/basic', $tpl);
	}
	
	
	function Login()
	{
		if(isset($_POST['login']))
		{
			log_message('debug', "AuthTaMeR Class Post Login received");
			$this->load->library('validation');
			$rules['alias']	= "trim|xss_clean|required|alpha_numeric";
			$rules['password']	= "trim|xss_clean|required|alpha_numeric";
			$this->validation->set_rules($rules);
			$data['alias'] = 'Alias';
			$data['password'] = 'Password';
			$this->validation->set_fields($data);
			
			if ($this->validation->run() == TRUE)
			{
				$rules['captcha'] = "callback_captcha_check";
				$this->validation->set_rules($rules) ;
				
				if ($this->validation->run() == TRUE)
				{
					$rules['login'] = "callback_login_check";
					$this->validation->set_rules($rules) ;
					
					if ($this->validation->run() == FALSE)
					{
						$data['tpltitle'] = 'Login';
						$data['captcha'] = $this->captcha_view();
						$data['tplmenu'] = $this->guestMenu(); 
						$this->load->view('account/login',$data);
					}
				}else{
					$data['tpltitle'] = 'Login';
					$data['captcha'] = $this->captcha_view();
					$data['tplmenu'] = $this->guestMenu(); 
					$this->load->view('account/login',$data);
				}
			}else{
				$data['tpltitle'] = 'Login';
				$data['captcha'] = $this->captcha_view();
				$data['tplmenu'] = $this->guestMenu(); 
				$this->load->view('account/login',$data);
			}
		}else{
			$data['tpltitle'] = 'Login';
			$data['captcha'] = $this->captcha_view();
			$data['tplmenu'] = $this->guestMenu(); 
			$this->load->view('account/login', $data);
		}
	}
}
?>
